/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain;

import domain.exceptions.TryingToRemoveNonExistingFlagException;
import domain.Game.action;
import domain.exceptions.CannotMoveThereException;
import java.util.Iterator;
import java.util.Vector;
/**
 *
 * @author shaigi
 */
public class AgentGreedy extends Agent{

//    public AgentGreedy(String name,action lastDirection, int arsenal, String assignment) {
//        super(name, lastDirection, arsenal, assignment);
//    }
    public AgentGreedy(String name,action lastDirection, String assignment) {
        super(name, lastDirection, assignment);
    }
    //copy constructor
    public AgentGreedy(AgentGreedy other){
    	super(other);
    }
    
    /*
    @Override
    public void die(Board board) {
        try {
            Game._board.removeAgent(getSquare().getRow(), getSquare().getColumn());
            System.out.println(this.toString() + " has been shot and removed from board. RIP");
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
    */
    
    @Override
    public void play(Board board, Vector<Agent> agents) {
        Vector<Flag> flags = board.getFlags();
        Flag destination = getClosestFlag(flags);
        action dir = getDirection(destination);
        if (dir != null){
            try {
                move(dir, board, agents);
            } catch (CannotMoveThereException ex) {
                System.out.println(getName()+ "says: \"Can't move there cause I'm greedy\"");
            }
        } else {
            System.err.println("This line shouldn't be reached");
        }
        if ( getSquare().isContainFlag() ){
            try {
                takeFlag(getSquare(), board);
            } catch (TryingToRemoveNonExistingFlagException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    private Flag getClosestFlag(Vector<Flag> flags) {
        Flag ans = null;
        int initValue       = Math.max(Game._board.getNumberOfColumns(), Game._board.getNumberOfRows());
        int rowsDeltaMin       = initValue;
        int columnsDeltaMin    = initValue;
        int distanceMin        = rowsDeltaMin + columnsDeltaMin;
        for (Iterator<Flag> it = flags.iterator(); it.hasNext();) {
            Flag flag = it.next();
            int rowsDeltaCurrent    = Math.abs(getSquare().getRow() - flag.getSquare().getRow());
            int columnsDeltaCurrent = Math.abs(getSquare().getColumn() - flag.getSquare().getColumn());
            int distanceCurrent     = rowsDeltaCurrent + columnsDeltaCurrent;
            if (distanceMin > distanceCurrent){
                distanceMin = distanceCurrent;
                ans = flag;
            }
        }
        return ans;
    }

    private action getDirection(Flag destinationFlag) {
        action ans;
        int rowFlag      = destinationFlag.getSquare().getRow();
        int columnFlag   = destinationFlag.getSquare().getColumn();
        int rowAgent     = getSquare().getRow();
        int columnAgent  = getSquare().getColumn();
        int rowsDelta    = rowFlag - rowAgent;
        int columnsDelta = columnFlag - columnAgent;
        
        System.out.println("Flag: row "+rowFlag+" col "+columnFlag);
        System.out.println("Agent: row "+rowAgent+" col "+columnAgent);
        
        if ( columnsDelta > 0 ){
            ans = action.RIGHT;
        } else if ( columnsDelta < 0 ){
            ans = action.LEFT;
        } else if ( rowsDelta > 0 ){
            ans = action.DOWN;
        } else if ( rowsDelta < 0 ) {
            ans = action.UP;
        } else {
            System.out.println("Located on FLAG");
            return null;
        }
        return ans;
    }
    
    @Override
    public boolean equals(Object other){
    	if(!(other instanceof AgentGreedy)){
    		return false;
    	}
    	else{
    		return super.equals(other);
    	}
    }
}
